package com.zyk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zyk.entity.Organization;
import com.zyk.mapper.OrganizationMapper;
import com.zyk.service.IOrganizationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zyk.vo.FinalResult;
import com.zyk.vo.OrganizationVo;
import com.zyk.vo.OrganizationVo2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组织机构 服务实现类
 * </p>
 *
 * @author feifeimao
 * @since 2017-11-20
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;

    /**
     * tree
     * @return
     */
    public List<OrganizationVo> findAll() {
        List<Organization> organizations = super.selectList(new EntityWrapper<Organization>(null));
        List<OrganizationVo> voList = new ArrayList<OrganizationVo>();
        for (Organization or : organizations) {
            OrganizationVo vo = new OrganizationVo();
            vo.setId(or.getId());
            vo.setInconCls(or.getIcon());
            vo.setText(or.getName());
            vo.setPid(or.getPid());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * treeGrid
     * @return
     */
    public List<OrganizationVo2> treeGrid() {
        List<Organization> organizations = this.selectList(new EntityWrapper<Organization>(null));
        List<OrganizationVo2> vos = new ArrayList<OrganizationVo2>();
        for (Organization or : organizations) {
            OrganizationVo2 vo = new OrganizationVo2();
            BeanUtils.copyProperties(or, vo);
            vo.setIconCls(or.getIcon());
            vos.add(vo);
        }
        return vos;
    }

    /**
     * edit
     * @param organization
     * @return
     */
    @Transactional
    public FinalResult edit(Organization organization) {
        int update = organizationMapper.update(organization);
        if (update > 0) {
            return new FinalResult("编辑成功", true);
        } else {
            return new FinalResult("编辑失败", false);
        }
    }
}
